import request from '@/utils/request'
import {
  SpuData,
  SpuResponseData,
  AllTrademark,
  SpuImageResponseData,
  SpuSaleAttrResponseData,
  BaseSpuSaleAttrResponseData,
  SkuData,
  SkuInfoData,
} from './type'

enum API {
  // 获取SPU列表
  GETSPU_URL = '/admin/product/',
  // 获取全部品牌
  GETALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  // 获取SPU的图片列表
  GETSPUIMAGELIST_URL = '/admin/product/spuImageList/',
  // 获取SPU的销售属性列表
  GETSPUSALEATTRLIST_URL = '/admin/product/spuSaleAttrList/',
  // 获取全部销售属性[颜色、版本、尺码]
  GETBASESALEATTRLIST_URL = '/admin/product/baseSaleAttrList',
  // 新增SPU
  ADDSPUINFO_URL = '/admin/product/saveSpuInfo',
  // 修改SPU
  UPDATESPUINFO_URL = '/admin/product/updateSpuInfo',
  // 删除SPU
  REMOVESPU_URL = '/admin/product/deleteSpu/',
  // 新增SKU
  ADDSKUINFO_URL = '/admin/product/saveSkuInfo',
  // 根据SPU查找SKU
  GETSKUINFO_URL = '/admin/product/findBySpuId/',
}
// 获取SPU列表
export const getSpuListApi = (
  page: number,
  limit: number,
  category3Id: number | string,
) =>
  request.get<any, SpuResponseData>(
    API.GETSPU_URL + `${page}/${limit}?category3Id=${category3Id}`,
  )
// 获取全部品牌
export const getAllTrademarkApi = () =>
  request.get<any, AllTrademark>(API.GETALLTRADEMARK_URL)
// 获取SPU的图片列表
export const getSpuImageListApi = (spuId: number) =>
  request.get<any, SpuImageResponseData>(API.GETSPUIMAGELIST_URL + spuId)
// 获取SPU的销售属性列表
export const getSpuSaleAttrListApi = (spuId: number) =>
  request.get<any, SpuSaleAttrResponseData>(API.GETSPUSALEATTRLIST_URL + spuId)
// 获取全部销售属性
export const getBaseSaleAttrListApi = () =>
  request.get<any, BaseSpuSaleAttrResponseData>(API.GETBASESALEATTRLIST_URL)
// 新增SPU
export const addOrUpdateSpuApi = (data: SpuData) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATESPUINFO_URL, data)
  } else {
    return request.post<any, any>(API.ADDSPUINFO_URL, data)
  }
}
// 删除SPU
export const removeSpuApi = (spuId: number | string) =>
  request.delete<any, any>(API.REMOVESPU_URL + spuId)
// 新增SKU
export const addOrUpdateSkuApi = (data: SkuData) =>
  request.post<any, any>(API.ADDSKUINFO_URL, data)
// 根据SPU查找SKU
export const getSkuInfoListApi = (spuId: number | string) =>
  request.get<any, SkuInfoData>(API.GETSKUINFO_URL + spuId)
